//引入vue
import Vue from "vue"
//实例化vue对象
const app = new Vue()

/**
 * 跳转操作
 */
function action(alias, requestParams = {}, action = 'navigate', authenticate = false, animationType = 'slide-in-right', authenticateAlias = '')
{
	//获取配置参数
	const envConfig = process.projectEnv;
	//整理链接信息
	var url = '', tabPages = ['/pages/index/index', '/pages/cart/cart', '/pages/account/index', '/pages/local/local', '/pages/task/task', '/pages/community/community']; 
	//根据标识获取链接
	switch (alias) {
		case 'oauth':
		case 'oauth.oauth':
			//登录
			url = '/pages/oauth/oauth';
		break;
		case 'index':
			//首页
			url = '/pages/index/index';
		break;
		
		case 'goods.detail':
			//商品详情
			url = '/pages/goods/detail';
		break;
		case 'goods.lists':
			// 商品列表
			url = '/pages/goods/lists';
		break;
		case 'goods.categories':
			// 全部商品
			url = '/pages/goods/categories';
		break;
		case 'cart.cart':
			//购物车
			url = '/pages/cart/cart';
		break;
		case 'cart.settlement':
			//购物车
			url = '/pages/cart/settlement';
		break;
		case 'cashier':
			//
			url = '/pages/cashier/cashier';
		break;
		
		case 'register.register':
		//设置链接
			url = '/pages/' + alias.replace('.', '/');
			// url = url.replace('.', '/');
		break;
		
		case 'account.index':
		case 'account.invite':
		case 'account.settings':
		case 'account.open_membership':
		case 'account.open_merchant':
		
		case 'merchant.index':
		case 'merchant.exchange':
		case 'merchant.franchise':
		//设置链接
			url = '/pages/' + alias.replace('.', '/');
			// url = url.replace('.', '/');
			authenticate = true;
		break;
		
		case 'teams.index':
		case 'teams.custom':
		case 'teams.log':
		case 'teams.size_zone':
		
		case 'user_score.index':
		case 'user_score.logs':
		case 'user_score.transfer':
		case 'user_score.withdrawals':
		
		case 'orders.lists':
		case 'orders.detail':
		
		case 'wallets.index':
		
		//设置链接
			url = '/pages/account/' + alias.replace('.', '/');console.log(alias);
			// url = url.replace('.', '/');
			//设置必须登录
			authenticate = true;
		break;
		
		
		
		
		
		
		
		
		
		// 	//设置链接
		// 	url = '/pages/' + alias.replace('.', '/');
		// 	url = url.replace('.', '/');
		// 	//设置必须登录
		// 	authenticate = true;
		// break;
		// case 'orders':
		// 	//设置链接
		// 	url = '/pages/account/orders/lists';
		// 	//设置必须登录
		// 	authenticate = true;
		// break;
		// case 'orders.detail':
		// case 'orders.service':
		// case 'orders.apply':
		// case 'orders.refund':
		// case 'orders.barter':
		// case 'orders.consult':
		// case 'orders.comment':
		// 	//设置链接
		// 	url = '/pages/account/'+alias.replace('.', '/');
		// 	//设置必须登录
		// 	authenticate = true;
		// break;
		// case 'merchant.merchant':
		// case 'merchant.cashier':
		// case 'merchant.log':
		// case 'merchant.team':
		// case 'merchant.lists':
		
		// case 'merchant.reward':
		// case 'merchant.online':
		// case 'merchant.withdraw':
		// case 'merchant.info':
		
		// case 'category.category':
		// 	//设置链接
		// 	url = '/pages/' + alias.replace('.', '/');
		// 	//设置必须登录
		// 	// authenticate = true;
		// break;
		// case 'coins.statistics':
		// 	//设置链接
		// 	url = '/pages/' + alias.replace('.', '/');
		// break;
		// case 'team.team':
		// case 'team.trans_balance':
		// case 'team.trans_coin':
		// 	//设置链接
		// 	url = '/pages/' + alias.replace('.', '/');
		// 	//设置必须登录
		// 	authenticate = true;
		// break;
		case 'back':
			//判断是否存在跳转链接
			if (typeof (requestParams.__redirect_uri__) !== 'undefined' && requestParams.__redirect_uri__.length > 0) {
				//设置跳转链接
				url = requestParams.__redirect_uri__;
				//设置为正常跳转
				action = 'navigate';
			} else {
				//获取当前路由信息
				var current_pages = uni.$u.pages();
				//判断历史记录长度
				if (current_pages.length <= 1) {
					//跳转首页
					// url = '/pages/index/index';
					url = '/pages/account/index';
					//设置为返回
					action = 'switch';
				} else {
					//设置链接
					url = '__BACK_TO_LAST_PAGE__';
					//设置为返回
					action = 'back';
				}
			}
			console.log(url);
		break;
		case 'none':
			//直接返回，不操作
			return true;
		break;
		default:
			url = (typeof alias !== 'undefined' && alias.length > 0 ? alias : '/pages/index/index');
		break;
	}
	//检测是否需要验证登录
	if (authenticate && !app.nii_common.auth.check()) {
		//截断请求
		return false;
	}
	//判断操作链接是否为tabbar对象
	if (tabPages.includes(url)) {
		//设置操作方式为switch
		action = 'switch';
	}
	//判断是否存在跳转参数
	if (!uni.$u.test.isEmpty(requestParams)) {
		//追加参数
		url += uni.$u.queryParams(requestParams, !url.includes('?'));
	}
	//判断是否开发环境
	if (envConfig.APP_ENV !== 'production') {
		//调试信息
		console.log(url);
		console.log(action);
	}
	//根据操作方式跳转
	switch (action) {
		case 'redirect':
			//跳转链接
			uni.redirectTo({
				url: url,
				animationType: animationType
			});
		break;
		case 'switch':
			console.log(url);
			//跳转链接
			uni.switchTab({
				url: url,
				animationType: animationType
			});
		break;
		case 'launch':
			//跳转链接
			uni.reLaunch({
				url: url
			});
		break;
		case 'back':
			//更改动画方式
			animationType = animationType.replace('-in', '-out');
			//返回
			uni.navigateBack({
				animationType: animationType
			})
		break;
		default:
			//跳转链接
			uni.navigateTo({
				url: url,
				animationType: animationType
			});
		break;
	}
	//返回成功
	return true;
}


//暴露方法
module.exports = {
	action: action
}